from flask import Blueprint,redirect,render_template,json,request,abort,current_app
import os


lantu=Blueprint('blue',__name__)

@lantu.route('/favicon.ico')                     #避免找不到图标报错
def favicon():
    # 返回一个空的响应
    return '', 204


@lantu.route('/<path:subpath>/<pagename>')  # 捕获路径参数,避免使用load时报 404
def page(subpath, pagename):
    # 安全地加入路径，避免路径遍历攻击
    safe_subpath = os.path.normpath(subpath)
    safe_pagename = os.path.basename(pagename)

    # 构建模板文件路径
    template_path = safe_join(safe_subpath, safe_pagename)
    
    # 输出构建的路径用于调试
    print(f"Requested template path: {template_path}")

    # 确保模板文件存在
    full_template_path = os.path.join(current_app.template_folder, template_path)
    if not os.path.exists(full_template_path):
        print(f"Template not found: {full_template_path}")
        abort(404)

    try:
        return render_template(template_path)
    except Exception as e:
        print(f"Error loading template {template_path}: {e}")
        abort(404)


from . import subpage